<?php 
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use cmf\controller\BaseController;
use think\Db;
use app\admin\service\Rsa2Service;
use app\admin\controller\WxloginController;
	class WxloginController extends BaseController
	{
		
		function login(){
			// $obj=new RedisController;
			// $obj -> delete();
		// $data=array('appid'=>'wx0f912ae0a6bd22ea&secret=428d9485b1b696adc863e647ddcb29ce','js_code'=>'001mwwL92ZmAcO02PPL92NvuL92mwwLI','grant_type'=>'authorization_code');
		$url='http://v2.crejoy.cn/user/login/user_info';
		$token = $this->request->param('token');
		// db()
		$data['token']=$token;
		$data['appid']='erp';
		$data['nonce']=nonce();
		$data['sign'] = Rsa2Service::createSign($data,config('private_key'));
		$info = sqcurl($url, 'post', $data, array());
		$info=json_decode($info,true);
		// echo count($info['data']['data']);
		//var_dump($info);
		// exit;
		if(empty($info['data']['id'])){
			return "登陆失败";
		}
		$focus=db('focus')->where('app_userid',$info['data']['id'])->find();
		if($focus){
			db('focus')->update(array('is_focus'=>$info['data']['is_focus_gzh'],'app_userid'=>$info['data']['id'],'id'=>$focus['id']));
		}else{
			db('focus')->insert(array('is_focus'=>$info['data']['is_focus_gzh'],'app_userid'=>$info['data']['id']));
		}
		
		$staff_info=db('staff_member')->where('app_userid',$info['data']['id'])->field('id,business_card,staff_name,staff_phone,app_company_name,app_position')->find();
	
		//exit;
		if($staff_info){
		
			// return info_back(array('staff_id'=>$staff_id),'请求成功',true);
			return json_encode($staff_info);


		}else{




			// return info_back(array(),'请求失败',false);
			$add_info['app_userid']=$info['data']['id'];
			$add_info['app_user_type']=$info['data']['user_type'];
			$add_info['app_user_email']=$info['data']['user_email'];
			$add_info['app_sex']=$info['data']['sex'];
			$add_info['app_user_nickname']=$info['data']['user_nickname'];
			$add_info['app_staff_mobile']=$info['data']['mobile'];
			$add_info['app_create_time']=$info['data']['create_time'];
			$add_info['app_update_time']=$info['data']['update_time'];
			$add_info['app_area_id']=$info['data']['area_id'];
			$add_info['app_industry']=$info['data']['industry_id'];
			$add_info['app_position']=$info['data']['position'];
			$add_info['app_company_name']=$info['data']['company_name'];
			$add_info['app_company_tel']=$info['data']['company_tel'];
			$add_info['app_web_url']=$info['data']['web_url'];
			$add_info['app_logo']=$info['data']['logo'];
			$add_info['app_address']=$info['data']['address'];
			$add_info['app_business']=$info['data']['business'];
			$add_info['app_check_status']=$info['data']['check_status'];
			$add_info['app_user_login']=$info['data']['user_login'];
			$add_info['app_user_pass']=$info['data']['user_pass'];
			$add_info['app_user_status']=$info['data']['user_status'];
			$add_info['app_im_token']=$info['data']['im_token'];
			$add_info['app_accid']=$info['data']['accid'];
			$add_info['app_coin']=$info['data']['coin'];
			$add_info['app_avatar']=$info['data']['avatar'];
			$add_info['app_test_sign']=$info['data']['test_sign'];
			$add_info['app_receive_coin']=$info['data']['receive_coin'];
			$add_info['app_ali_id']=$info['data']['ali_id'];
			$add_info['app_ali_name']=$info['data']['ali_name'];
			$add_info['app_token']=$info['data']['token'];
			$add_info['app_limit_amount']=$info['data']['limit_amount'];
			$staff_id=db('staff_member')->insert($add_info);
			if($staff_id){
				$staff_info=db('staff_member')->where('app_userid',$info['data']['id'])->field('id,business_card,staff_name,staff_phone,app_company_name,app_position,app_avatar,customer_id')->find();
				// return db('staff_member')->getLastInsID();
				return json_encode($staff_info);


			}else{
				return '用户信息更新失败';
			}

		}
		
		// var_dump($add_info);

	}














































	function add_staff_info(){
		//var_dump($_REQUEST);
		
		$data['id'] = $this->request->param('staff_id');
		$business_card = $this->request->param('business_card','');
		$form_id = $this->request->param('form_id','');
		$i = 0;		
		if($business_card){
			$data['business_card']=$business_card;
			$value = db('staff_member')->where(array('id'=>$data['id'],'business_card'=>$business_card))->find();
			if(empty($value)){
				$data['apply_member']=3;
			}
			
		}
	
		//$apply_data['is_audit'] = $this->request->param('is_audit',1);
		$staff_name=$this->request->param('staff_name');
		if($staff_name){
			$data['staff_name']=$staff_name;
		}
		$staff_phone=$this->request->param('staff_phone');
		if($staff_phone){
			$data['staff_phone']=$staff_phone;
		}
		
		//$app_company_name=$this->request->param('company_name');
		// if($app_company_name){
		// 	$data['app_company_name']=$app_company_name;
		// }
		$app_position=$this->request->param('position');
		if($app_position){
			$data['app_position']=$app_position;
		}
		$customer_id=$this->request->param('customer_id');
		if($customer_id){
			$data['customer_id']=$customer_id;
			$values = db('staff_member')->where(array('id'=>$data['id'],'customer_id'=>$customer_id))->find();
			if(empty($values)){
				$data['apply_member']=3;
			}
		}else{
			return array('mess'=>'信息不完整','state'=>0);
		}
	
		// if($business_card || $customer_id){
		// 	$data['apply_member']=3;
		// }
		$site_prices= $this->request->param('site_prices');
		$service_prices = $this->request->param('service_price');
		
		$equipments = $this->request->param('equipments/a');
		$other_equipments = $this->request->param('other_equipments/a');
		
		if($site_prices){
			$apply_data['site_prices']=$site_prices;
		}	
		if($service_prices){
			$apply_data['service_prices']=$service_prices;
		}	
		$eq = '';
		$other_eq = '';
		if($equipments){
		
			if(is_array($equipments)){
				foreach ($equipments as $key => $value) {
					$eq .=  get_cate_name(16,$value) ;
				}
				// echo $eq;
				$apply_data['equipments']=implode(',', $equipments);
			}
		
		}	
		if($other_equipments){
			
			if(is_array($other_equipments)){
				foreach ($other_equipments as $k => $v) {
					$other_eq .=  get_cate_name(16,$v) ;
				} 
				//echo $other_eq;
					// file_put_contents('/hl/log.txt',print_r($other_eq,true),FILE_APPEND);
				$apply_data['other_equipments']=implode(',', $other_equipments);
			}
			
		}	
		$apply_data['site_id']=$this->request->param('site_id');
		$apply_data['staff_id']=$this->request->param('staff_id');
		$apply_data['start_time']=$this->request->param('start_time');
		$apply_data['end_time']=$this->request->param('end_time');
		$apply_data['add_time']=time();
		$remark=$this->request->param('remark','');
		$site_incubator = db('site')->where('id',$apply_data['site_id'])->value('incubator_id');
		$staff_incubator = db('staff_member')->where('id',$apply_data['staff_id'])->value('incubator_id');
		if($site_incubator != $staff_incubator){
			return json(array('stat'=>0,'mess'=>'不同园区不能相互预约'));
		}
		if($remark){
			$apply_data['remark']=$remark;
		}
		$apply_data['is_audit'] = $this->request->param('is_audit',1);
		if($apply_data['is_audit'] == 2){
			$apply_data['audit_status'] = 1;
		}else{
			$status=db('staff_member')->where('id',$data['id'])->value('apply_member');
			if($status == 2 || $status == 3){
				$apply_data['audit_status'] = 1;
			}else{
				$apply_data['audit_status'] = 2;
			}
			
		}
	
		
		if($apply_data['start_time'] > $apply_data['end_time']){
			$a = $apply_data['start_time'];
			$apply_data['start_time'] = $apply_data['end_time'];
			$apply_data['end_time'] = $a ;
		}
		
	
		
		if(empty($data['id']) && empty($data['business_card']) &&  empty($data['staff_name']) && empty($data['staff_phone'])  && empty($data['app_position'])){
			return '确认预约信息不完整';
		}
		
		$sure = db('staff_member')->update($data);
		$apply=db('site_apply_info')->insert($apply_data);
		$apply_id = db('site_apply_info')->getLastInsID();
		// $pay_info = $this->doPay($apply_id);
		//var_dump($pay_info);
		$site_id=$this->request->param('site_id');
		$prices = db('site')->alias('s')->join('incubator i','s.incubator_id = i.id')->field('i.site_service_price,s.price,s.site_name')->where('s.id',$site_id)->find();

		$staff_info = db('staff_member')->alias('s')->join('customer c','s.customer_id = c.id')->where('s.id',$apply_data['staff_id'])->field('c.incubator_id,s.staff_name,s.apply_member,c.is_free')->find();
		// var_dump($staff_info);
		// 	echo db()->getLastsql();
// 		 file_put_contents('/hl/log.txt',print_r('bbbbbbb',true),FILE_APPEND);
// 		  file_put_contents('/hl/log.txt',print_r($prices['site_service_price'],true),FILE_APPEND);
// file_put_contents('/hl/log.txt',print_r('aaaaaaa',true),FILE_APPEND);
//  file_put_contents('/hl/log.txt',print_r($prices['price'],true),FILE_APPEND);
		if($staff_info['is_free']==2 || ($apply && intval($prices['site_service_price'])==0 && intval($prices['price'])==0)){
		
			// file_put_contents('/hl/log.txt',print_r('11111',true),FILE_APPEND);
			if($apply_data['is_audit'] == 2){
				//echo 22222;
				
				// $staff_info = db('staff_member')->alias('s')->join('customer c','s.customer_id = c.id')->where('s.id',$apply_data['staff_id'])->field('c.incubator_id,s.staff_name')->find();
				
			//	var_dump($staff_info);
				$site_type_id = db('site')->where('id',$apply_data['site_id'])->field('site_type_id,site_name')->find();
				// echo db('site')->getLastsql();
				$receive_infos = db('user')->alias('u')->join('role_user r' ,'u.id = r.user_id')->where(array('u.incubator_id'=> $staff_info['incubator_id'],'r.role_id'=>4))->field('u.receive_site,u.receive_user,u.id,u.app_userid')->select();
				// file_put_contents('/hl/log.txt',print_r('2222221',true),FILE_APPEND);
			
				 //var_dump($receive_info);
			//	echo $site_type_id .'###';
				 // file_put_contents('/hl/log.txt',print_r('55555',true),FILE_APPEND);
				 // file_put_contents('/hl/log.txt',print_r($receive_infos,true),FILE_APPEND);
				// file_put_contents('/hl/log.txt',print_r($site_type_id['site_type_id'],true),FILE_APPEND);
				// file_put_contents('/hl/log.txt',print_r('ccccccc',true),FILE_APPEND);
				// file_put_contents('/hl/log.txt',print_r($receive_infos,true),FILE_APPEND);
				foreach ($receive_infos as $key => $receive_info) {
						# code...
					// file_put_contents('/hl/log.txt',print_r('aaaaqqq',true),FILE_APPEND);

					if(in_array($site_type_id['site_type_id'], explode(',', $receive_info['receive_site']))){
						// file_put_contents('/hl/log.txt',print_r('3333',true),FILE_APPEND);
						$first='有会议室预约请求待审核';
						$keyword1 = $staff_info['staff_name'];
						$keyword2 = $site_type_id['site_name'];
						$keyword3 = date("Y-m-d H:i",$apply_data['start_time']).'--'.date("Y-m-d H:i",$apply_data['end_time']);
						$remark = ' 备注：  固有设备：'.$eq.'  可选设备：'.$other_eq.'留言： '.$remark.'。请尽快处理';
						$url = config('gz_send_templete');
						$url1 = config('meeting_audit_list').'?wx_secrect='.$receive_info['id'];
							// file_put_contents('/hl/log.txt',print_r('**5555555555555555566666666677777778%%%%',true),FILE_APPEND);
							// file_put_contents('/hl/log.txt',print_r($url1,true),FILE_APPEND);
						$return_info = push_wx_info($receive_info['app_userid'],$first,$keyword1,$keyword2,$keyword3,$remark,$url,$url1);
					}
				}
			}else{
			
				// $staff_info = db('staff_member')->alias('s')->join('customer c','s.customer_id = c.id')->where('s.id',$apply_data['staff_id'])->field('c.incubator_id,s.staff_name,s.apply_member')->find();
			
				if($staff_info['apply_member'] ==3){
				
					$site_type_id = db('site')->where('id',$apply_data['site_id'])->field('site_type_id,site_name')->find();
			
					$receive_infos = db('user')->alias('u')->join('role_user r' ,'u.id = r.user_id')->where(array('u.incubator_id'=> $staff_info['incubator_id'],'r.role_id'=>4))->field('u.id,u.receive_site,u.receive_user,u.id,u.app_userid')->select();
					// 	echo db()->getLastsql();
					// var_dump($receive_infos);
					//file_put_contents('/hl/log.txt',print_r('*&*^^^121',true),FILE_APPEND);
					//file_put_contents('/hl/log.txt',print_r($receive_infos,true),FILE_APPEND);
					foreach ($receive_infos as $key => $receive_info) {
						# code...

					//var_dump($receive_info);
			//	echo $site_type_id .'###';
						if($receive_info['receive_user'] ==2 ){

							$first='有企业员工待审核';
							$keyword1 = $staff_info['staff_name'];
							$keyword2 = $site_type_id['site_name'];
							$keyword3 = date("Y-m-d H:i",$apply_data['start_time']).'--'.date("Y-m-d H:i",$apply_data['end_time']);
							$remark =' 备注：  固有设备：'.$eq.'  可选设备：'.$other_eq.' 留言： '. $remark.'。请尽快处理';
							$url = config('gz_send_templete');
							$url1 =  config('staff_audit_list').'?wx_secrect='.$receive_info['id'];
							//  file_put_contents('/hl/log.txt',print_r('**5555555555555555566666666677777778%%%%',true),FILE_APPEND);
							// file_put_contents('/hl/log.txt',print_r($url1,true),FILE_APPEND);
							$return_info = push_wx_info($receive_info['app_userid'],$first,$keyword1,$keyword2,$keyword3,$remark,$url,$url1);

							//file_put_contents('/hl/log.txt',print_r($return_info,true),FILE_APPEND);
						}
					}
				}else{
					if($eq || $other_eq || $remark){





						// $staff_info = db('staff_member')->alias('s')->join('customer c','s.customer_id = c.id')->where('s.id',$apply_data['staff_id'])->field('c.incubator_id,s.staff_name')->find();
						$site_type_id = db('site')->where('id',$apply_data['site_id'])->field('site_type_id,site_name')->find();
						$receive_infos = db('user')->alias('u')->join('role_user r' ,'u.id = r.user_id')->where(array('u.incubator_id'=> $staff_info['incubator_id'],'r.role_id'=>4))->field('u.receive_site,u.receive_user,u.id,u.app_userid')->select();
						foreach ($receive_infos as $key => $receive_info) {
							if(in_array($site_type_id['site_type_id'], explode(',', $receive_info['receive_site']))){
								$first='有会议室预约请求待审阅';
								$keyword1 = $staff_info['staff_name'];
								$keyword2 = $site_type_id['site_name'];
								$keyword3 = date("Y-m-d H:i",$apply_data['start_time']).'--'.date("Y-m-d H:i",$apply_data['end_time']);
								$remark = ' 备注：  固有设备：'.$eq.'  可选设备：'.$other_eq.' 留言：'.$remark.'请尽快处理';
								$url = config('gz_send_templete');
								$url1 = config('meeting_audit_list').'?wx_secrect='.$receive_info['id'];
								$return_info = push_wx_info($receive_info['app_userid'],$first,$keyword1,$keyword2,$keyword3,$remark,$url,$url1);
							}
						}

					}
				}
			}
			if($form_id){
				// file_put_contents('/hl/log.txt',print_r('&&&&&&$',true),FILE_APPEND);
				// file_put_contents('/hl/log.txt',print_r($form_id,true),FILE_APPEND);
				// file_put_contents('/hl/log.txt',print_r('&&&&&&$',true),FILE_APPEND);
				$site_name = db('site')->where('id',$apply_data['site_id'])->value('site_name');
				$app_userid = db('staff_member')->where('id',$data['id'])->value('app_userid');
				$is_focus = db('focus')->where('app_userid',$app_userid)->value('is_focus');
				$remark = '未关注公众账号，点击进入小程序进入个人页面进行关注';
				// file_put_contents('/hl/log.txt',print_r('%%%%%',true),FILE_APPEND);
				// file_put_contents('/hl/log.txt',print_r($is_focus,true),FILE_APPEND);
				// file_put_contents('/hl/log.txt',print_r('%%%%%',true),FILE_APPEND);
				if($is_focus != 1){
					// file_put_contents('/hl/log.txt',print_r('******1#1',true),FILE_APPEND);
					$info =send_info($site_name,$remark,date("Y-m-d H:i",$apply_data['start_time']).'--'.date("Y-m-d H:i",$apply_data['end_time']),'pages/mine/mine',$app_userid,$form_id);
		
				}
			}
			if($apply){
				return 1;
			}else{
				return 0;
			}
			
		}else{
			$pay_info = $this->doPay($apply_id);
			if($pay_info){
				return $pay_info;
			}else{
				return 0;
			}
		}

	}
	function editStaff(){
		$data['business_card'] = $this->request->param('business_card');
		$data['id'] = $this->request->param('staff_id',0);
		$data['staff_name']=$this->request->param('staff_name');
		$data['staff_phone']=$this->request->param('staff_phone');
		// $data['app_company_name']=$this->request->param('company_name');
		$data['app_position']=$this->request->param('app_position');
		$data['customer_id']=$this->request->param('customer_id');
		$data['incubator_id']=$this->request->param('incubator_id');
		$staff_info = db('staff_member')->where('id',$data['id'])->find();
		$incubator_id = db('customer')->where('id',$data['customer_id'])->value('incubator_id');
		if($incubator_id != $data['incubator_id']){
			return json(array('stat'=>0,'mess'=>'当前公司不属于该园区'));
		}
		if($staff_info['customer_id'] != $data['customer_id'] || $staff_info['incubator_id'] != $data['incubator_id'] || !empty($data['business_card'])){
			$data['apply_member']=3;
		}
		
		$update=db('staff_member')->update($data);

		if($update){
			// return 1;
			return $this->get_userinfos($data['id']);
		}else{
			return 0;
		}
	
	}
	function tip(){
		$where=array();
		$arr=array();
		$str = $this->request->param('customer_name',''); 
		$incubator_id = $this->request->param('incubator_id',''); 
		if(!empty($incubator_id)){
			$where['incubator_id'] = $incubator_id;
		}else{
			$where['incubator_id'] =1;
		}
		$where['status']=1;
		$str_array=db('customer')->where('customer_name','like','%'.$str.'%')->where($where)->limit(10)->select();
		
		foreach ($str_array as $key => $value) {
			$arr[$key]['customer_id']=$value['id'];
			$arr[$key]['customer_name']=$value['customer_name'];
		}
		return json($arr);
		// $str1=implode(',', $arr);
		// echo $str1;  
	}




	function get_userinfos($staff_id){
		// $data['id'] = $this->request->param('staff_id');
		$data['id'] = $staff_id;
		$staff_info=db('staff_member')->where('id',$data['id'])->field('id,business_card,staff_name,staff_name,staff_phone,app_company_name,app_position,app_avatar,app_logo,customer_id')->find();
		if(!$staff_info){
			return '该员工不存在';
		}
		$staff_info['customer_name']=get_customer_name($staff_info['customer_id']);
		$where['staff_id'] = $data['id'];
		$where['audit_status'] = 2;
		$where1['staff_id'] = $data['id'];
		$where1['audit_status'] = 3;
		//$where2=' staff_id = "'.$data['staff_id'].'" and start_time > "'.time().'" ' ;
		$where2=' staff_id = "'.$data['id'].'" and start_time > "'.time().'" ' ;
		$condition1='(is_audit = 1 ) or (is_audit =2 and audit_status =2)';
		$condition=' start_time <= "'.time().'" and end_time >= "'.time().'" and staff_id = "'.$data['id'].'" ';
		$arr['cancel_num']['num']= db('site_apply_info')->where($where1)->count();
		$arr['cancel_num']['name']='取消';
		$arr['cancel_num']['type']=3;
		$arr['using_num']['num']= db('site_apply_info')->where($condition)->where($condition1)->count();
		$arr['using_num']['name']='正在使用';
		$arr['using_num']['type']=2;
		$arr['allow_num']['num']=db('site_apply_info')->where($where2)->where($condition1)->count();
		$arr['allow_num']['name']='预订成功';
		$arr['allow_num']['type']=1;
		$staff_info=array_merge($staff_info,$arr);
		return json($staff_info);
	}

	function get_userinfo(){
		$data['id'] = $this->request->param('staff_id');
		$site_id = $this->request->param('site_id','');
		$staff_info=db('staff_member')->where('id',$data['id'])->field('id,business_card,staff_name,staff_name,staff_phone,app_company_name,app_position,app_avatar,app_logo,customer_id,incubator_id')->find();
		if(!empty($staff_info['incubator_id'])){
			$staff_info['incubator_name'] = db('incubator')->where('id',$staff_info['incubator_id'])->value('incubator_name');
		}
		if(!$staff_info){
			return '该员工不存在';
		}
		if($site_id){
			// echo 2222;
			// exit;
			$site_address = db('site')->where('id',$site_id)->value('address');
			// var_dump($site_address);
			// exit;
			$staff_info['site_address'] = $site_address ;
			// var_dump($staff_info);
			// exit;
		}else{
			$staff_info['site_address'] = '暂无' ;
		}
		$staff_info['customer_name']=get_customer_name($staff_info['customer_id']);
		$where['staff_id'] = $data['id'];
		$where['audit_status'] = 2;
		$where1['staff_id'] = $data['id'];
		$where1['audit_status'] = 3;

		//$where2=' staff_id = "'.$data['staff_id'].'" and start_time > "'.time().'" ' ;
		$where2=' staff_id = "'.$data['id'].'" and start_time > "'.time().'" ' ;
		// $condition1='(is_audit = 1 ) or (is_audit =2 and audit_status =2)';
		$condition1=' audit_status =2 ';
		$condition=' start_time <= "'.time().'" and end_time >= "'.time().'" and staff_id = "'.$data['id'].'" ';
		$condition2=' audit_status =1 ';
		$condition3='  end_time >= "'.time().'" and staff_id = "'.$data['id'].'" ';
		$arr['cancel_num']['num']= db('site_apply_info')->where($where1)->count();
		$arr['cancel_num']['name']='取消';
		$arr['cancel_num']['type']=3;
		$arr['using_num']['num']= db('site_apply_info')->where($condition)->where($condition1)->count();
		$arr['using_num']['name']='正在使用';
		$arr['using_num']['type']=2;

		$arr['auditing_num']['num']= db('site_apply_info')->where($condition2)->where($condition3)->count();
		$arr['auditing_num']['name']='审核中';
		$arr['auditing_num']['type']=4;

		$arr['allow_num']['num']=db('site_apply_info')->where($where2)->where($condition1)->count();
		$arr['allow_num']['name']='预订成功';
		$arr['allow_num']['type']=1;
		$staff_info=array_merge($staff_info,$arr);
		return json($staff_info);
	}















function test(){
	if($_POST){
		$b='';
		$a=[1,23,4,56,7,8];
		$b.='<form action="" method="post">
		服务器地址';
		foreach ($a as $key => $value) {
			$b.='<input type="text" name="host" value="'.$value.'" />';
		}


		
		$b.='<input type="text" name="host" value="localhost" /><br />
		数据库账号：<input type="text" name="user" value="" /><br />
		数据库密码：<input type="password" name="pwd" value="" /><br />
		指定数据库：<input type="text" name="db" value="test" /><br />
		<input type="submit" value="确定"/>
	    </form>';
	    echo $b;
    }
}



function bind(){

	$back_info = $_REQUEST;
			// var_dump($back_info);
			// exit;
	$code=$back_info['code'];
			//$code = '011nQ18Q1qjUO71UXbaQ1Zxn8Q1nQ18v';
	$url = "http://v2.crejoy.cn/api/login/check_login_wx?code=$code&type=2";
			//echo $url;
	$info = sqcurl($url, 'get', array(), array());
	$arr = json_decode(json_encode(json_decode($info)),true);
	if(!empty($back_info['ext'])){
		header("Content-Type: text/html; charset=utf8");
		echo '<div width="50%" height="50%" vertical-align="middle" align="center"><span>'.$arr['data']['user_nickname'].',请长按二维码在弹出菜单中选择识别图中二维码关注创享公众号</span><br/><img width="500px" height="500px" src="https://img.crejoy.cn/qrcode_for_crejoy_service_platform.jpg"/></div>';
		exit;
	}
	
			//var_dump($arr['data']);
	$data['id'] = $back_info['user_id'];
	$data['app_userid'] = $arr['data']['id'] ;
	$is_has = db('user')->where('id',$back_info['user_id'])->find();
	if($is_has['app_userid']){
		if($is_has['app_userid'] == $arr['data']['id']){

			return '已经绑定，无需重复绑定';
		}
		$data['wx_nickname'] = $arr['data']['user_nickname'];
		$update=db('user')->update($data);
		return '已经更绑';
	}
	$data['wx_nickname'] = $arr['data']['user_nickname'];
	$update=db('user')->update($data);
	if($update){
		header("Content-Type: text/html; charset=utf8");
		return '绑定成功。 <div width="50%" height="50%" vertical-align="middle" align="center"><span>'.$arr['data']['user_nickname'].',请长按二维码在弹出菜单中选择识别图中二维码关注创享公众号</span><br/><img width="500px" height="500px" src="https://img.crejoy.cn/qrcode_for_crejoy_service_platform.jpg"/></div>';
	}else{
		return '绑定失败';
	}
			//db('incubator')->
			// var_dump($arr);



}




/*发起支付*/
public function doPay($apply_id){
	//$apply_id = 1;
	$site_id=$this->request->param('site_id');
	$prices = db('site')->alias('s')->join('incubator i','s.incubator_id = i.id')->field('i.site_service_price,s.price,s.site_name')->where('s.id',$site_id)->find();
	if(($prices['site_service_price']!=0 || $prices['price']!=0) && !empty($this->request->param('token'))){
		$start_time=$this->request->param('start_time');
		$end_time=$this->request->param('end_time');
		$week = date("w",$start_time);
	
		$hours = ($end_time - $start_time)/(60*60);
		$now_hour = date("H",$end_time);
		$now_min = date("i",$end_time);
		$rate = $hours/4;
		$whole_up = ceil($rate);
		$whole_down = floor($rate);
		$site_rent = $whole_up*$prices['price']; 
	
		$service_price = 0;
		// for($i=1;$i<= $whole_up;$i++){
		// 	if($whole_up == 1){
		// 		if($now_hour <= 17 && ($week!= 6 || $week!=0)){
		// 			$service_price = $prices['site_service_price'];
		// 		}
		// 		elseif($now_hour <= 17 && ($week== 6 || $week==0)){
		// 			$service_price = $prices['site_service_price']*2;
		// 		}
		// 		if($now_hour > 17 && ($week!= 6 || $week!=0) ){
		// 			$service_price = $prices['site_service_price']*1.5;
		// 		}elseif($week== 6 || $week==0){
		// 			$service_price = $prices['site_service_price']*2;
		// 		}
		// 	}
		// 	elseif($i==1 && ($week!= 6 || $week!=0)){
		// 		$service_price += $prices['site_service_price'];
		// 	}elseif($i!=1 && ($week!= 6 || $week!=0)){
		// 		$service_price += $prices['site_service_price']*2;
		// 	}elseif($week== 6 || $week==0){
	 //          $service_price += $prices['site_service_price']*1.5;
  //           }
  //       }
		$start_hour = date('H',$start_time);
		$start_min = date('i',$start_time);	
		// file_put_contents('/hl/log.txt',print_r($whole_up,true),FILE_APPEND);
		// file_put_contents('/hl/log.txt',print_r('!!!!',true),FILE_APPEND);
		// 	file_put_contents('/hl/log.txt',print_r($now_min,true),FILE_APPEND);
		// 		file_put_contents('/hl/log.txt',print_r('$$$$',true),FILE_APPEND);
		// 			file_put_contents('/hl/log.txt',print_r($now_hour,true),FILE_APPEND);
		// 				file_put_contents('/hl/log.txt',print_r('****',true),FILE_APPEND);

		for($i=1;$i<= $whole_up;$i++){
			if($whole_up == 1){
				if(intval($now_min)){
				
					if($now_hour < 17 && ($week!= 6 || $week!=0)){
					
						$service_price = $prices['site_service_price'];
					}
					if($now_hour >= 17 && ($week!= 6 || $week!=0) ){
						$service_price = $prices['site_service_price']*1.5;
					}elseif($week== 6 || $week==0){
						$service_price = $prices['site_service_price']*1.5;
					}
				}else{

					if($now_hour <= 17 && ($week!= 6 || $week!=0)){
						$service_price = $prices['site_service_price'];
					}
					if($now_hour > 17 && ($week!= 6 || $week!=0) ){
						$service_price = $prices['site_service_price']*1.5;
					}elseif($week== 6 || $week==0){

						$service_price = $prices['site_service_price']*1.5;
					}
				}

			}else{
				if($week != 6 && $week!=0){
					// $now_hour -=4;
					if(intval($now_min)){
						if($now_hour >=17){

							$service_price += $prices['site_service_price']*1.5;
						}else{

							$service_price += $prices['site_service_price'];
						}
					}else{

						if($now_hour >17){

							$service_price += $prices['site_service_price']*1.5;
						}else{

							$service_price += $prices['site_service_price'];
						}
					}
					$now_hour -=4;

				}elseif($week== 6 || $week==0){
					$service_price += $prices['site_service_price']*1.5;
				}
			}

		}

        $data['amount'] = $site_rent+$service_price;

        $data['amount'] = round($data['amount'],2);
		$url = config('pay_url');
		$data['body'] = $prices['site_name'];
		$data['subject'] = $prices['site_name'];
		$data['type'] = 4;
			// $data['amount'] = $this->request->param('amount');
		$data['token'] = $this->request->param('token');
		file_put_contents('/hl/log.txt',print_r('ssssssssss',true),FILE_APPEND);
		file_put_contents('/hl/log.txt',print_r($data,true),FILE_APPEND);
		$info = sqcurl($url, 'post', $data, array());
		// var_dump($data);
		// var_dump($info);
		file_put_contents('/hl/log.txt',print_r($info,true),FILE_APPEND);
		file_put_contents('/hl/log.txt',print_r('rrrrrrrr',true),FILE_APPEND);
		if(json_decode($info)->code == 0){
			return $info;
		}

		$order_info = json_decode($info)->data->order_info;
		$order_arr = json_decode($info)->data->order_arr;
		$back['app_id'] = $order_arr->appId;
		$back['nonce_str'] = $order_arr->nonceStr;
		$back['package'] = $order_arr->package;
		$back['sign_type'] = $order_arr->signType;
		$back['time_stamp'] = (string)$order_arr->timeStamp;
		$back['pay_sign'] = $order_arr->paySign;
		$back['order_sn'] = $order_info->order_sn;
		$back['amount'] = $order_info->amount;
		$back['apply_id'] = $apply_id;
		db('orderlist')->insert($back);
		// file_put_contents('/hl/log.txt',print_r($back,true),FILE_APPEND);
		return json($back);
    }else{
    	return 0;
    }




	


}









function send(){
	$common = new WxcommonController;
	$common->send_mess();
}






































   }